package com.data
{
	import mx.collections.ArrayCollection;
	import mx.collections.GroupingCollection;
	import mx.core.Application;
	import mx.events.CollectionEvent;
	
	
	public class InitData
	{
		[Bindable]
		public var data:GroupingCollection  	= new GroupingCollection();
		[Bindable]
		public var projects:GroupingCollection  	= new GroupingCollection();
		[Bindable]
		public var tasks:ArrayCollection	= new ArrayCollection();
		
		[Bindable]
		public var currentTasks:ArrayCollection	= new ArrayCollection();
		
		[Bindable]
		public var arrPriorityChar:ArrayCollection = new ArrayCollection(["A", "B", "C", "D"]);
		
		public function InitData()
		{
			
			//var task1:ProjectVO = new ProjectVO(1, "Project 1", new Date(), new Date(), false);
			var task1:CalendarProject = new CalendarProject(1, "Project 1", new Date(), new Date(), false,0xBB0000);
			
			var task2:TaskVO 	= new TaskVO(1, task1, "Task 1 - Coffee", new Date(2009,2, 12, 9, 30), new Date(2009,2,12,10,30), false);
			var task3:TaskVO 	= new TaskVO(2, task1, "Task 2 - Status Meeting", new Date(2009,2, 11, 11, 0), new Date(2009,2,11,12,0), true);
			var task4:TaskVO 	= new TaskVO(3, task1, "Task 3 - Gym", new Date(2009,2, 16, 17, 30), new Date(2009,2,16,19,0), false);
			
			//var task10:ProjectVO = new ProjectVO(2, "Project 2", new Date(), new Date(), false);
			var task10:CalendarProject = new CalendarProject(2, "Project 2", new Date(), new Date(), false, 0x00BB00);
			var task11:TaskVO 	= new TaskVO(4, task10, "Task 1 for Project 2", new Date(2009,2, 17, 7, 30), new Date(2009,2,17,9,0), false);
			var task12:TaskVO 	= new TaskVO(5, task10, "Task 2 for Project 2", new Date(2009, 2, 9, 14, 0),  new Date(2009, 2, 9, 14, 30), true);
			var task13:TaskVO 	= new TaskVO(6, task10, "Task 3", new Date(2009, 2, 14, 14, 15),  new Date(2009, 2, 15, 14, 30), true);
			var task14:TaskVO 	= new TaskVO(7, task10, "Task 4 - Rebiuld", new Date(2009, 2, 12, 17, 0),  new Date(2009, 2, 12, 17, 30), true);
			
			this.tasks.addItem(task2);
			this.tasks.addItem(task4);
			this.tasks.addItem(task11);
			this.tasks.addItem(task12);
			this.tasks.addItem(task13);
			this.tasks.addItem(task14);
			
			this.data.source = [task2, task3, task4, task11, task12, task13, task14];
			this.projects.source = [task1, task10];
			this.tasks.enableAutoUpdate();
			
			//currentTasks.removeAll();
			var date:Date = new Date();
			var dateYear	= date.getFullYear();
			var dateMonth	= date.getMonth();
			var dateDate	= date.getDate();
			for (var i:int = 0; i < tasks.length; i++) {
				var stCDate:Date = tasks[i].startDate;
				var dueCDate:Date = tasks[i].startDate;
				if ((stCDate.getFullYear() <= dateYear)&&(stCDate.getMonth() <= dateMonth)
        			&& (stCDate.getDate() <= dateDate) &&(dueCDate.getDate() >= dateDate)
        			&& (dueCDate.getFullYear() >= dateYear)&&(dueCDate.getMonth() >= dateMonth)) {
        			this.currentTasks.addItem(this.tasks[i]);
        		} 
        	} 
			this.tasks.addEventListener(CollectionEvent.COLLECTION_CHANGE, tasksChange);
			this.setPriorityTasks();
		}
		
		public function setPriorityTasks():void{
			this.tasks[0].priorityChar = "A";
			this.tasks[0].priorityNumber = 1;
			
			this.tasks[1].priorityChar = "B";
			this.tasks[1].priorityNumber = 1;
			
			this.tasks[2].priorityChar = "B";
			this.tasks[2].priorityNumber = 2;
			
			this.tasks[3].priorityChar = "A";
			this.tasks[3].priorityNumber = 2;
			
			this.tasks[4].priorityChar = "C";
			this.tasks[4].priorityNumber = 1;
		}
		
		private function tasksChange(event:CollectionEvent):void {
			setCurrentTask(Application.application.selectDate);
			Application.application.projectViewId.updateData();
			Application.application.homePageViewId.updateData();
		}
		
		public function tasksChangeEmpty():void {
			setCurrentTask(Application.application.selectDate);
			Application.application.projectViewId.updateData();
			Application.application.homePageViewId.updateData();
		}
		
		public function changeTask(id:int, task:TaskVO):void {
			for (var i:int = 0; i < this.tasks.length; i++){
				if (this.tasks[i].id == id){
					this.tasks.setItemAt(task, i);
				}
			}
		}
		
		public function setCurrentTask(date:Date):void{
			var dateYear	= date.getFullYear();
			var dateMonth	= date.getMonth();
			var dateDate	= date.getDate();
			Application.application.mainDataId.currentTasks.removeAll();
        	for (var i:int = 0; i < tasks.length; i++) {
        		var stCDate:Date 	= tasks[i].startDate;
				var dueCDate:Date 	= tasks[i].dueDate;
				if ((stCDate.getFullYear() <= dateYear)&&(stCDate.getMonth() <= dateMonth)
        			&& (stCDate.getDate() <= dateDate) &&(dueCDate.getDate() >= dateDate)
        			&& (dueCDate.getFullYear() >= dateYear)&&(dueCDate.getMonth() >= dateMonth)) {
        			Application.application.mainDataId.currentTasks.addItem(this.tasks[i]);
        		} 
        	}
        	
		}
		
		public function getCompletedTaskByProjName(project:String):Number{
			var count:Number = 0;
			for (var i:int = 0; i < this.tasks.length;i++){
				if ((tasks[i].projectName == project) && (tasks[i].isCompleted)){
					count++;
				}
			}
			return count;
		}
		
		public function getNotCompletedTaskByProjName(project:String):Number{
			var count:Number = 0;
			for (var i:int = 0; i < this.tasks.length;i++){
				if ((tasks[i].projectName == project) && (!tasks[i].isCompleted)){
					count++;
				}
			}
			return count;
		}
		
		public function setCompletedTask(id:Number, isCompl:Boolean):void{
			for (var i:int = 0; i < this.tasks.length; i++){
				if (this.tasks[i].id == id){
					var task:TaskVO = tasks[i] as TaskVO;
					task.isCompleted = isCompl;
					this.tasks.setItemAt(task, i);
				}
			}
		}
		
		public function getProjectByName(name:String):ProjectVO{
			for (var i:int = 0; i < this.tasks.length; i++){
				if (this.tasks[i].projectName == name){
					return tasks[i].project;
				}
			}
			return null;
		} 
		
		public function changeTaskDate(id:int, start:Date, end:Date):void {
			for (var i:int = 0; i < Application.application.mainDataId.tasks.length; i++){
				if (Application.application.mainDataId.tasks[i].id == id){
					Application.application.mainDataId.tasks[i].startDate = start;
					Application.application.mainDataId.tasks[i].dueDate = end;
				}
			}
		}
		
		public function addNewTask(name:String, start:Date, end:Date, proj:ProjectVO, priorCh:String, priorN:Number, summaryMessage:String):void{
			var id:Number = 0;
			for (var i:int = 0; i < this.tasks.length; i++){
				if (id < this.tasks[i].id){
					id = this.tasks[i].id;
				}
			}
			var task:TaskVO = new TaskVO((id + 1) as int, proj,name, start, end, false);
			task.priorityChar 	= priorCh;
			task.priorityNumber	= priorN;
			//task.summaryMessage = summaryMessage;
			task.summaryMessage	= summaryMessage;
			this.tasks.addItem(task);
		}

	}
}